package edu.je.column.service;

import com.junqson.je.common.model.ApiResult;
import com.junqson.je.common.util.AppUtil;
import edu.je.column.model.ArticleIndexModel;
import edu.je.column.model.ArticleModel;
import edu.je.column.model.Vo.ArticleVo;
import edu.je.column.repo.ArticleIndexRepo;
import edu.je.column.repo.ArticleRepo;
import edu.je.column.repo.AuthorInfoRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.Lob;
import java.sql.SQLException;
import java.util.List;

/**
 * @author MengXin
 * @description:
 * @program: je-gateway
 * @date 2021/4/22 15:05
 * @mail mx@radish.cloud
 */
@Service
public class ArticleService {
    @Autowired
    ArticleRepo articleRepo;
    @Autowired
    ArticleIndexRepo articleIndexRepo;
    @Autowired
    AuthorInfoRepo authorInfoRepo;
    public ArticleModel addArticle(Long authorId,String articleTitle,Long commentId,String articleContent){
        ArticleModel addModel = new ArticleModel();
        addModel.setArticleTitle(articleTitle);
        addModel.setAuthorId(authorId);
        System.out.println(articleContent);
        addModel.setArticleContent(articleContent);
        addModel.setCommentId(commentId);
        try {
            articleRepo.save(addModel);
        }catch (Exception e){
            System.out.println(e);
            return null;
        }
        return addModel;
    }

    public ArticleVo getArticleById(Long ArticleId){
        ArticleModel articleModelList= articleRepo.findById(ArticleId).get();
        ArticleVo articleVo = new ArticleVo();
        AppUtil.copyPropertiesIgnoreNull(articleModelList, articleVo);
        articleVo.setAuthorInfo(authorInfoRepo.findById(articleModelList.getAuthorId()).get());
        return articleVo;
    }

    public List<ArticleIndexModel> getArticleIndex(Long columnId){
        List<ArticleIndexModel> articleModelList= articleIndexRepo.findAllByColumId(columnId);
        return articleModelList;
    }
}
